package com.nimitz.modules.approval.controller;


import com.nimitz.common.core.constant.UserConstants;
import com.nimitz.common.core.web.controller.BaseController;
import com.nimitz.common.core.web.domain.AjaxResult;
import com.nimitz.modules.approval.domain.ApprovalProcedure;
import com.nimitz.modules.approval.service.IApprovalProcedureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ApprovalProcedureController
 * @Author FuCun
 * @Date 2021-08-17-14:45
 * @PackageName com.nimitz.modules.approval.controller
 * @Description 审批流
 * @Version 1.0.0
 */
@RestController
@Api(tags = "审批流管理")
@RequestMapping("/approval-procedure")
public class ApprovalProcedureController  extends BaseController {

    @Autowired
    private IApprovalProcedureService approvalProcedureService;


    /**
     * 查询所有审批流（含搜索）
     *
     * @param  approvalProcedure 审批流
     * @return 审批流集合
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有审批流（含搜索）")
    public AjaxResult list(ApprovalProcedure approvalProcedure){
        List<ApprovalProcedure> approvalProcedures=approvalProcedureService.selectApprovalProcedureList(approvalProcedure);
        return AjaxResult.success(approvalProcedures);
    }

    /**
     * 新增审批流信息
     *
     * @param approvalProcedure 审批流
     * @return 结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增审批流信息")
    public AjaxResult add(@RequestBody ApprovalProcedure approvalProcedure) {
        if (UserConstants.NOT_UNIQUE.equals(approvalProcedureService.checkApprovalProcedureNameUnique(approvalProcedure.getProcedureName()))) {
            return AjaxResult.error("新增审批流'" + approvalProcedure.getProcedureName() + "'失败，审批流名称已存在");
        }
        //该处的创建者应该为登录用户的名称，暂时写死在这里。
        approvalProcedure.setProcedureName("admin");
        return toAjax(approvalProcedureService.insertApprovalProcedure(approvalProcedure));
    }

    /**
     * 修改审批流信息
     *
     * @param approvalProcedure 审批流
     * @return 结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改审批流信息")
    public AjaxResult edit(@RequestBody ApprovalProcedure approvalProcedure) {
        if (UserConstants.NOT_UNIQUE.equals(approvalProcedureService.checkApprovalProcedureNameUnique(approvalProcedure.getProcedureName()))) {
            return AjaxResult.error("修改审批流'" + approvalProcedure.getProcedureName() + "'失败，审批流名称已存在");
        }
        approvalProcedure.setProcedureName("admin");
        return toAjax(approvalProcedureService.updateApprovalProcedure(approvalProcedure));
    }


    /**
     * 删除审批流信息
     *
     * @param procedureId 流id
     * @return 结果
     */
    @DeleteMapping("/{procedureId}")
    @ApiOperation(value = "删除审批流信息")
    public AjaxResult remove(@PathVariable Long procedureId) {
        return toAjax(approvalProcedureService.deleteApprovalProcedureByProcedureId(procedureId));
    }


}
